import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        toolBar=QToolBar()
        btn=QPushButton('Cpoy Text')
        btn.clicked.connect(self.CopyText)
        toolBar.addWidget(btn)

        btn=QPushButton('Cpoy Url')
        btn.clicked.connect(self.CopyUrl)
        toolBar.addWidget(btn)

        btn=QPushButton('Cpoy Image')
        btn.clicked.connect(self.CopyImage)
        toolBar.addWidget(btn)

        btn=QPushButton('Cpoy Color')
        btn.clicked.connect(self.CopyColor)
        toolBar.addWidget(btn)

        btn=QPushButton('Cpoy Html')
        btn.clicked.connect(self.CopyHtml)
        toolBar.addWidget(btn)

        btn=QPushButton('Paste')
        btn.clicked.connect(self.Paste)
        toolBar.addWidget(btn)
        layout.addWidget(toolBar)

        self.typeLabel=QLabel('type')
        layout.addWidget(self.typeLabel)

        self.contentLabel=QLabel('content')
        layout.addWidget(self.contentLabel)

        self.mimeLabel=QLabel('mime')
        layout.addWidget(self.mimeLabel)

        self.clipboard=QApplication.clipboard()
        self.clipboard.dataChanged.connect(self.ClipboardChanged)

        self.setLayout(layout)
    def CopyText(self):
        self.clipboard.setText('clipped Text')
    def CopyUrl(self):
        mimeData=QMimeData()
        mimeData.setUrls(['https://www.baidu.com/'])
        self.clipboard.setMimeData(mimeData)
    def CopyImage(self):
        self.clipboard.setPixmap(QPixmap(f'{self.parent.getAppDir()}/resources/qtforpython.png'))
    def CopyColor(self):
        mimeData=QMimeData()
        mimeData.setColorData(Qt.GlobalColor.red)        
        self.clipboard.setMimeData(mimeData)
    def CopyHtml(self):
        mimeData=QMimeData()
        mimeData.setHtml('<b>Bold and <font color=red>Red</font></b>')
        self.clipboard.setMimeData(mimeData)
    def Paste(self):
        self.typeLabel.setText('')
        mimeData=self.clipboard.mimeData()
        if mimeData.hasImage():
            self.contentLabel.setPixmap(mimeData.imageData())
            self.typeLabel.setText('Image')
        elif mimeData.hasColor():
            self.contentLabel.setText('Color Foreground')
            palette=QPalette()
            palette.setColor(QPalette.ColorRole.WindowText,mimeData.colorData())
            self.contentLabel.setPalette(palette)            
            self.typeLabel.setText('Color')
        elif mimeData.hasUrls():
            url=mimeData.urls()[0]            
            self.contentLabel.setText(url.toString())
            self.typeLabel.setText('Url')
        elif mimeData.hasHtml():
            self.contentLabel.setText(mimeData.html())
            self.contentLabel.setTextFormat(Qt.TextFormat.RichText)
            self.typeLabel.setText('Html')
        elif mimeData.hasText():
            self.contentLabel.setText(mimeData.text())
            self.contentLabel.setTextFormat(Qt.TextFormat.PlainText)
            self.typeLabel.setText('Text')
        else:
            pass
    def ClipboardChanged(self):
        mimeData=self.clipboard.mimeData()        
        _str=''
        for format in mimeData.formats():
            data=mimeData.data(format)
            _str=_str+'\n'+format+' : '+str(data.data())
        self.mimeLabel.setText(_str)


def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet